package com.ymttest.business.service;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

import com.sun.org.apache.bcel.internal.util.Class2HTML;
import com.ymatou.iapi.activitymanage.service.returnProductActivityStockCall;
import com.ymatou.iapi.fastturnover.parameter.FastTurnoverWithdrawApplyReq;
import com.ymatou.iapi.fastturnover.parameter.VerifyApplyBillReq;
import com.ymatou.iapi.fastturnover.parameter.addBean;
import com.ymatou.iapi.fastturnover.parameter.closeBean;
import com.ymatou.iapi.fastturnover.parameter.bean.AccountInfo;
import com.ymatou.iapi.fastturnover.parameter.bean.WithdrawApplyDetailVo;
import com.ymatou.iapi.fastturnover.service.FastTurnoverWithdrawApplyCall;
import com.ymatou.iapi.fastturnover.service.VerifyApplyBillCall;
import com.ymatou.iapi.fastturnover.service.addCall;
import com.ymatou.iapi.fastturnover.service.closeCall;
import com.ymt.base.YmatouEasyCall;
import com.ymttest.database.model.biinputdb.SellerLevelHistory;
import com.ymttest.database.sqlwapper.billinputdb.SellerLevelHistoryWrapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class FastTurnoverService {
	
	private static FastTurnoverWithdrawApplyReq fastTurnoverWithdrawApplyReq = new FastTurnoverWithdrawApplyReq();
	private static FastTurnoverWithdrawApplyCall fastTurnoverWithdrawApplyCall = new FastTurnoverWithdrawApplyCall();
	
	private static VerifyApplyBillReq verifyApplyBillReq = new VerifyApplyBillReq();
	private static VerifyApplyBillCall verifyApplyBillCall = new VerifyApplyBillCall();
	
	private static SellerLevelHistoryWrapper sellerLevelHistoryWrapper = new SellerLevelHistoryWrapper();
	
	/**
	 * 申请快周转单
	 * @throws Exception
	 */
	public static void addTurnoverApply() throws Exception {
		
		Long sellerId = 20227567L;
		
		SellerLevelHistory sellerLevelHistory = new SellerLevelHistory();
		
		sellerLevelHistory.setDDate(YMTDateUtil.getDate());
		sellerLevelHistory.setSellerID(sellerId.intValue());
		sellerLevelHistory.setUpdateTime(YMTDateUtil.getDate());
		sellerLevelHistory.setUnconfirm_amount(BigDecimal.TEN);
		
		sellerLevelHistoryWrapper.insertSelective(sellerLevelHistory);
	
		List<WithdrawApplyDetailVo> withdrawApplyDetailVos = new ArrayList<>();
		
		WithdrawApplyDetailVo withdrawApplyDetailVo = new WithdrawApplyDetailVo();
		AccountInfo accountInfo = new AccountInfo();
		
		String accountid1 = AccountCallServiceV2.getAccountId(sellerId.intValue(), 1);
		AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
				"20000"), BigDecimal.ZERO);
		
		// 买手提现银行账号
		accountInfo.setAccountNo("dd369e93-5198-45c7-b4d5-fc27c702606a");
		accountInfo.setType(1);
		
		withdrawApplyDetailVo.setAccountInfo(accountInfo);
		withdrawApplyDetailVo.setApplyAmount(new BigDecimal("10001.00"));
		withdrawApplyDetailVo.setSettlementCurrency(4);
		
		withdrawApplyDetailVos.add(withdrawApplyDetailVo);
		
		fastTurnoverWithdrawApplyReq.setSellerId(sellerId);
		fastTurnoverWithdrawApplyReq.setWithdrawCurrency(1);
		fastTurnoverWithdrawApplyReq.setWithdrawApplyDetailVos(withdrawApplyDetailVos);

		// 发送请求
		fastTurnoverWithdrawApplyCall.setData(fastTurnoverWithdrawApplyReq);
		fastTurnoverWithdrawApplyCall.callService();
	}
	
	/**
	 * 审核快周转单
	 * @throws Exception
	 */
	public static void verifyTurnoverApply(List<String> applyNos) throws Exception {
	
		verifyApplyBillReq.setApplyNoList(applyNos);
		verifyApplyBillReq.setAgree(true);
		verifyApplyBillReq.setMemo(TestDataManager.randomStr(10));

		// 发送请求
		verifyApplyBillCall.setData(verifyApplyBillReq);
		verifyApplyBillCall.callService();
	}
	
	/**
	 * 导出支付结果成功通知
	 * @param applyDetailNo
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void onApplyDetailExportPayEvent(String applyDetailNo)
			throws ClientProtocolException, IOException {

		YmatouEasyCall onApplyDetailExportPayEventCall = new YmatouEasyCall("",
				"POST", "JSON");
		StringBuilder onApplyDetailExportPayEventString = new StringBuilder();

		onApplyDetailExportPayEventString.append("{\"applyDetailNo\":\""
				+ applyDetailNo + "\"}");

		onApplyDetailExportPayEventCall
				.setUrl("http://fastturnover.iapi.ymatou.com/api/onApplyDetailExportPayEvent");

		onApplyDetailExportPayEventCall
				.setData(onApplyDetailExportPayEventString.toString());

		onApplyDetailExportPayEventCall.callService();
	}
	
	/**
	 * 导入支付结果成功通知
	 * @param applyDetailNo
	 * @param status
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void onApplyDetailImportPayEvent(String applyDetailNo, String payBizNo, Integer status)
			throws ClientProtocolException, IOException {

		YmatouEasyCall onApplyDetailImportPayEventCall = new YmatouEasyCall("",
				"POST", "JSON");
		StringBuilder onApplyDetailImportPayEventString = new StringBuilder();

		onApplyDetailImportPayEventString.append("{\"applyDetailNo\":\""
				+ applyDetailNo + "\",");
		onApplyDetailImportPayEventString.append("\"bizNo\":\""
				+ applyDetailNo + "\",");
		onApplyDetailImportPayEventString.append("\"status\":"
				+ status + ",");
		onApplyDetailImportPayEventString.append("\"payTime\":\""
				+ YMTDateUtil.getBeforeOrNextHour(-1, YMTDateUtil.YMDHMS) + "\"}");

		onApplyDetailImportPayEventCall
				.setUrl("http://fastturnover.iapi.ymatou.com/api/onApplyDetailImportPayEvent");

		onApplyDetailImportPayEventCall
				.setData(onApplyDetailImportPayEventString.toString());

		onApplyDetailImportPayEventCall.callService();
	}
	
	/**
	 * 自动还款
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static void autoRepayJob()
			throws ClientProtocolException, IOException {

		YmatouEasyCall autoRepayJobCall = new YmatouEasyCall("",
				"POST", "JSON");
		StringBuilder autoRepayJobString = new StringBuilder();

		autoRepayJobString.append("{}");

		autoRepayJobCall
				.setUrl("http://fastturnover.iapi.ymatou.com/api/autoRepayJob");

		autoRepayJobCall
				.setData(autoRepayJobString.toString());

		autoRepayJobCall.callService();
	}
	
	
	
	public static addBean initAdd(Long sellerId) throws ParseException
	{
		addBean bean=new addBean();
		bean.setSellerId(sellerId);
		bean.setNikeName("自动化测试昵称_"+TestDataManager.getRandomIntNum(5));
		bean.setRate(1);
		bean.setRate(20);
		bean.setMaxLoan(true);
		bean.setOperator("autoTest");
	
		SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
		java.util.Date start=formatter.parse(LocalDateTime.now().plusDays(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		java.util.Date end=formatter.parse(LocalDateTime.now().plusDays(3).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		
		bean.setStartTime(start);
		bean.setEndTime(end);
		return  bean;
	}
	
	public static addCall AddCall(addBean bean) throws ClientProtocolException, IOException
	{
		addCall call=new addCall();
		call.setData(bean);
		call.callService();
		return call;
	}
	
	
	public static closeCall close(Long sellerId, String operator) throws ClientProtocolException, IOException
	{
		closeBean bean=new closeBean();
		closeCall call=new closeCall();
		bean.setSellerId(sellerId);
		bean.setOperator(operator);
		call.setData(bean);
		call.callService();	
		return call;
	}
	public static void main(String[] args) throws Exception {
		
		FastTurnoverService.addTurnoverApply();
	}
}
